<?php

class Songs_model extends CI_Model {

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();
    }

    function _loadAll() {
        $ress = $this->db->query("select * from m_song order by id DESC")->result_array();
        return $ress;
    }

    function _loadPage($offset, $limit) {
        $ress = $this->db->query("select * from m_song order by id DESC LIMIT $offset, $limit")->result_array();
        return $ress;
    }

    function _loadPageCount() {
        $ress = $this->db->query("select count(id) as jml from m_song")->row_array();
        return $ress;
    }
    
    function _getById($id){
        $ress = $this->db->query("select * from m_song where id = '$id'")->row_array();
        return $ress;
    }
    function _getAudio($id){
        $ress = $this->db->query("select path_file from m_song_file where id_song = '$id' AND type = 'audio'")->row_array();
        if(empty($ress['path_file'])) $ress['path_file'] = null;
        return $ress['path_file'];
    }
    function _getVideo($id){
        $ress = $this->db->query("select path_file from m_song_file where id_song = '$id' AND type = 'video'")->row_array();
        if(empty($ress['path_file'])) $ress['path_file'] = null;
        return $ress['path_file'];
    }
    
    function _getRTSP($id){
       
        $server =  $this->db->query("select rtsp_server from config")->row_array();
        $path_audio = UPLOAD_PATH_AUDIO;
        $path_video = UPLOAD_PATH_VIDEO;
        
        $rtsp = array();
        
        $ress = $this->db->query("select path_file from m_song_file where id_song = '$id' AND type = 'video'")->row_array();
        if(empty($ress['path_file'])) $ress['path_file'] = null;
        $ext = substr($ress['path_file'], -3);
        $rtsp['video'] = $server['rtsp_server'].$ext.':'.str_replace($path_video.'/', '', $ress['path_file']);
        
        $ress = $this->db->query("select path_file from m_song_file where id_song = '$id' AND type = 'audio'")->row_array();
        if(empty($ress['path_file'])) $ress['path_file'] = null;
        $rtsp['audio'] = $server['rtsp_server'].'mp3:'.str_replace($path_audio.'/', '', $ress['path_file']);
        
        return $rtsp;
        
    }
    
    function _getMaxId() {
        $ress = $this->db->query("select max(id) as jml from m_song")->row_array();
        return $ress['jml'];
    }

    function uploadImage($file_name) {
        
        $config = array();
        $data = array();
        
        $config['upload_path'] = './' . UPLOAD_PATH_IMAGE;
        $config['allowed_types'] = 'gif|png|jpg|jpeg';
        $config['file_name'] = $file_name;
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);

        $data_upload = $_FILES["path_thumbnail"]['name'];
        $tipe_file = $_FILES["path_thumbnail"]['type'];
        $this->upload->initialize($config); 
        
        if (empty($data_upload)) {
            $data['path_thumbnail'] = '';
        } else {

                if (!$this->upload->do_upload('path_thumbnail')) {
                    $data['path_thumbnail'] = '';
                } else {
                    $upload_data = $this->upload->data();
                    $data['path_thumbnail'] = UPLOAD_PATH_IMAGE . '/' . $upload_data['file_name'];
                }
           
        }
        
        unset($config);
        
        return $data['path_thumbnail'];
    }
    
    function uploadAudio($file_name) {
        
        $config = array();
        $data = array();
        
        $config['upload_path'] = './' . UPLOAD_PATH_AUDIO;
        $config['allowed_types'] = 'mp3|mp4';
       // $config['allowed_types'] = 'gif|jpg|jpeg|flv|mp4|avi|mpeg|mov|mp3';
        $config['file_name'] = $file_name;
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);

        $data_upload = $_FILES["path_file_audio"]['name'];
        $tipe_file = $_FILES["path_file_audio"]['type'];
        $this->upload->initialize($config); 
        
        if (empty($data_upload)) {
            $data['path_file'] = '';
        } else {

                if (!$this->upload->do_upload('path_file_audio')) {
                    $data['path_file'] = '';
                } else {
                    $upload_data = $this->upload->data();
                    $data['path_file'] = UPLOAD_PATH_AUDIO . '/' . $upload_data['file_name'];
                }
             
        }
        
        unset($config);
        
        return $data['path_file'];
    }
    
    function uploadVideo($file_name) {
        
        $config = array();
        $data = array();
        
        $config['upload_path'] = './' . UPLOAD_PATH_VIDEO;
        $config['allowed_types'] = 'flv|mp4|avi|mpeg|mov';
      // $config['allowed_types'] = 'gif|jpg|jpeg|flv|mp4|avi|mpeg|mov|mp3';
        $config['file_name'] = $file_name;
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);

        $data_upload = $_FILES["path_file_video"]['name'];
        $tipe_file = $_FILES["path_file_video"]['type'];
        $this->upload->initialize($config); 
        
        if (empty($data_upload)) {
            $data['path_file'] = '';
        } else {

            if (!$this->upload->do_upload('path_file_video')) {
                    $data['path_file'] = '';
                } else {
                    $upload_data = $this->upload->data();
                    $data['path_file'] = UPLOAD_PATH_VIDEO . '/' . $upload_data['file_name'];
                }
        }
        
        unset($config);
        
        return $data['path_file'];
    }

    function _loadGenre() {
        $ress = $this->db->query("select genre from m_song group by genre order by genre ASC")->result_array();
        return $ress;
    }
    
    function _loadCountry() {
        $ress = $this->db->query("select country from m_song group by country order by country ASC")->result_array();
        return $ress;
    }
   
  
}

